package com.tuanzi.loan.business.service.settlement.impl;

import com.tuanzi.loan.business.entity.settlement.CwOutTicketInfo;
import com.tuanzi.loan.business.repository.settlement.CwOutTicketRepository;
import com.tuanzi.loan.business.service.settlement.CwOutTicketService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.service.CommonServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class CwOutTicketServiceImpl extends CommonServiceImpl<CwOutTicketInfo,Long> implements CwOutTicketService {

    @Autowired
    private CwOutTicketRepository cwOutTicketRepository;
    @Override
    public void saveOutTicketInfo(CwOutTicketInfo cwOutTicketInfo) {
        cwOutTicketRepository.save(cwOutTicketInfo);
    }

    @Override
    public CwOutTicketInfo getLatestOutTicketInfoByCompanyId(Long companyId) {
        List<CwOutTicketInfo> outTicketInfoList = cwOutTicketRepository.getOutTicketInfoByCompanyId(companyId);
        if(outTicketInfoList.size()==0){
            return null;
        }
        //拿到最新的信息
        CwOutTicketInfo cwOutTicketInfo = outTicketInfoList.stream().max((a,b) ->a.getId().intValue()-b.getId().intValue()).get();
        return cwOutTicketInfo;
    }

    @Override
    protected JpaRepository<CwOutTicketInfo, Long> getRepository() {
        return cwOutTicketRepository;
    }
}
